package com.android.lovingpet.adopt.host.hostinfo.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.adopt.host.hostinfo.HostInfoPresenter;
import com.android.lovingpet.adopt.host.hostinfo.IHostInfoContract;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.bean.HostFoster;
import com.android.lovingpet.util.Utility;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.android.lovingpet.adopt.host.hostinfo.HostInfoPresenter.FLAG_HOST_FOSTER;
import static com.android.lovingpet.adopt.host.hostinfo.HostInfoPresenter.FLAG_Undelivered_PET_ID;

/**
 * Created by ikata on 2018/5/20.
 */

public class FosterFinishFragment extends BaseFragment<IHostInfoContract.View, HostInfoPresenter> implements IHostInfoContract.View, View.OnClickListener {

    @BindView(R.id.bt_change)
    Button btChange;
    @BindView(R.id.bt_commit)
    Button btCommit;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.bt_cancel)
    Button btCancel;
    private int fosterId;
    private HostFoster hostFoster;

    private boolean PET_UNFINISHED = false;
    private int petId;
    Unbinder unbinder;

    @Override
    protected int setContentView() {
        return R.layout.fragment_foster_finish;
    }

    @Override
    protected void onCreateView(View view) {
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        initView();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    public void initView() {

        getPresenter().getUndeliveredPetId(Utility.getRealId(), 1, 10);
        //根据fragment数判断宠物是寄出成功
        if (getFragmentManager().getBackStackEntryCount() == 1) {
            PET_UNFINISHED = true;
        }
    }

    @Override
    public HostInfoPresenter createPresenter() {
        return new HostInfoPresenter();
    }

    @Override
    public IHostInfoContract.View createView() {
        return this;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void getPetId(Integer petId)
    {
        this.petId=petId;
        getPresenter().getHostFosterById(petId);
    }


    @OnClick({R.id.bt_commit, R.id.bt_change,R.id.bt_cancel})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_commit:
                getPresenter().updateState(fosterId, 1);
                EventBus.getDefault().postSticky(petId);
                break;
            case R.id.bt_change:
                if (PET_UNFINISHED) {
                    EventBus.getDefault().postSticky(hostFoster);
                    getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.host_info_content, new ContactWayFragment()).commit();
                } else {
                    getFragmentManager().popBackStack();
                }
                break;
                //取消寄养
            case R.id.bt_cancel:

                break;
        }
    }

    @Override
    public void next(@Nullable Bundle bundle) {
        //已送到
        if (bundle == null) {
            getFragmentManager().beginTransaction().replace(R.id.host_info_content, new BackHomeFragment()).commit();
            EventBus.getDefault().postSticky(hostFoster);
            return;
        }
        int flag = bundle.getInt("flag");
        switch (flag) {
            case FLAG_Undelivered_PET_ID:
                ArrayList<Integer> petList = bundle.getIntegerArrayList("ids");
                if (petList == null||petList.size()==0) {
                    return;
                }
                fosterId = petList.get(0);
                getPresenter().getHostFosterById(fosterId);
                break;
            case FLAG_HOST_FOSTER:
                hostFoster = bundle.getParcelable("hostFoster");
                runOnUiThread(() -> tvTime.setText(hostFoster.getEndTime()));
                break;
        }
    }
}
